angular.module('Circumference')
    .controller('Circumference.MagnifiersCtrl', ['$scope', '$state', '$ionicLoading', '$ionicPopup', '$ionicScrollDelegate', 'DevicePort', 'imanDialog', 'Device', 'Circumference.Param', 'Http', 'User',
        function ($scope, $state, $ionicLoading, $ionicPopup, $ionicScrollDelegate, DevicePort, imanDialog, Device, Param, Http, User) {
            $scope.selectObj = {};
            $scope.ee = {"code": ""};
            $scope.currentNumber = "";
            $scope.num = {
                "a": 1,
                "b": 2,
                "c": 3,
                "d": 4,
                "e": 5,
                "f": 6,
                "g": 7,
                "h": 8,
                "i": 9,
                "j": 0,
                "k": ".",
                "l": "C"
            };
            $scope.data.unit = 'g';

            $scope.$on('$ionicView.afterEnter', function () {
                $scope.param = Param.getParam();
            });

            var chunk = function (arr, size) {
                var newArr = [];
                for (var i = 0; i < arr.length - 1; i += size) {
                    newArr.push(arr.slice(i, i + size));
                }
                return newArr;
            };

            $scope.saveParam = function (type) {
                $ionicLoading.show();
                var realUser, realWorkLocation, realWorkLocationName;
                if (Param.getBarcode().Work === Device.get().Work._id) {
                    realUser = Param.getUsers();
                    realWorkLocation = Param.getBarcode().WorkLocation;
                    realWorkLocationName = Param.getBarcode().WorkLocationName;
                } else {
                    realUser =  [User.get()._id];
                    realWorkLocation = Device.get().WorkLocation._id;
                    realWorkLocationName = Device.get().WorkLocation.Name;
                }
                var localQCRecords = [];
                if (type === 'weight') {
                    localQCRecords.push($scope.param[type]);
                } else {
                    localQCRecords = $scope.param[type];
                }
                Http.post({
                    url: '/api/Wrapper/setWrapperMeasure',
                    data: {
                        Parameter: {
                            Barcode: Param.getBarcodeString(),
                            TerminalUniqueID: Device.getUUID(),
                            Work: Device.get().Work._id || 0,
                            WorkName: Device.get().Work.Name || '',
                            realWorkLocation: realWorkLocation || '',
                            realWorkLocationName: realWorkLocationName || '',
                            Users: realUser,
                            QCRecords: localQCRecords
                        }
                    }
                }).then(function () {
                    $ionicLoading.hide();
                    $scope.data.getQc = true;
                    Param.setParam({
                        key: type,
                        value: $scope.param[type]
                    });
                    if (type === 'magnifierCycle') {
                        Param.setParam({
                            key: 'cycle',
                            value: chunk($scope.param[type], 3)
                        });
                    }
                    $state.go('Circumference');
                    imanDialog.alert('确认成功', '提交成功');
                })['finally'](function () {
                    $ionicLoading.hide();
                });
            };

            $scope.cancelParam = function () {
                $state.go('Circumference');
            };

            $scope.setLocalObj = function (obj, index) {
                angular.element('.app' + Param.getActionIndex()).removeClass("selected");
                $scope.selectObj = obj;
                $scope.selectedRow = index;
                $scope.currentNumber = "";
                Param.setActionIndex(index);
            };

            $scope.doInput = function (n) {
                if (n === "C") {
                    $scope.currentNumber = "";
                } else if (n === ".") {
                    if ($scope.currentNumber !== "" && $scope.currentNumber.indexOf(".") === -1) {
                        $scope.currentNumber = $scope.currentNumber + "" + n;
                    }
                } else if (n === 0) {
                    if ($scope.currentNumber === "" || $scope.currentNumber !== 0 || $scope.currentNumber.indexOf(".") !== -1) {
                        $scope.currentNumber = $scope.currentNumber + "" + n;
                    }
                } else if (n !== 0 && n !== "." && n !== "C") {
                    if ($scope.currentNumber === "0") {
                        $scope.currentNumber = n;
                    } else {
                        $scope.currentNumber = $scope.currentNumber + "" + n;
                    }
                }
                $scope.selectObj.NormValue = Number($scope.currentNumber);
            };

            $scope.nextOne = function () {
                if (Param.getActionIndex() < Param.getParam().magnifierCycle.length - 1) {
                    if ($ionicScrollDelegate.getScrollView().__contentHeight > ($ionicScrollDelegate.getScrollView().__clientHeight +
                        $ionicScrollDelegate.getScrollPosition().top)) {
                        $ionicScrollDelegate.scrollBy(0, 45, true);
                    }
                    angular.element('.app' + Param.getActionIndex()).removeClass("selected");
                    Param.setActionIndex(Param.getActionIndex() + 1);
                    angular.element('.app' + Param.getActionIndex()).addClass("selected");
                    $scope.currentNumber = "";
                    $scope.selectObj = Param.getParam().magnifierCycle[Param.getActionIndex()];
                }
            };
        }
    ]);
